package cn.com.czpb.client.module.news.view;

import android.app.Activity;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import cn.com.czpb.client.R;
import cn.com.czpb.client.model.base.BaseEntity;
import cn.com.czpb.client.model.video.VideoItemBean;
import cn.com.czpb.client.module.video.utils.VideoPlayerHelper;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.services.NetWorkChangeReceiver;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.SPUtil;
import cn.com.czpb.client.utils.TUtils;

import java.util.HashMap;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/8/18.
 */

public class VideoHeadView implements View.OnClickListener {
    private Activity mActivity;
    private View root;
    private RelativeLayout rl_toppic_id;
    private ImageView img_toppic_id;
    private ImageView iv_start;
    private TextView tv_title_id;
    private TextView tv_vnum_id;
    private TextView tv_message;
    private TextView tv_start;
    private VideoItemBean mbean;
    private SPUtil spu;
    private String tid;
    private boolean isPraise = false;
    private boolean isDownPraise = false;
    private boolean isneed;


    private IntentFilter intentFilter;
    private NetWorkChangeReceiver netWorkChangeReceiver;
    private LocalBroadcastManager manager;

    public VideoHeadView(Activity mActivity, VideoItemBean bean, String tid, boolean isneed) {
        this.mActivity = mActivity;
        initView(bean);
        spu = SPUtil.getInstance();
        this.tid = tid;
        this.isneed = isneed;
        initNetWork();

    }

    private void initNetWork() {
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netWorkChangeReceiver = new NetWorkChangeReceiver();
        mActivity.registerReceiver(netWorkChangeReceiver, intentFilter);
    }

    public static VideoHeadView instance(Activity activity, VideoItemBean bean, String tid, boolean isneed) {
        return new VideoHeadView(activity, bean, tid, isneed);
    }


    public View getView() {
        return root;
    }

    private void initView(final VideoItemBean bean) {
        this.mbean = bean;
        LayoutInflater inflater = mActivity.getLayoutInflater();
        root = inflater.inflate(R.layout.view_videodetail_head, null);
        //顶部视频图片
        rl_toppic_id = root.findViewById(R.id.rl_toppic_id);
        img_toppic_id = root.findViewById(R.id.img_toppic_id);
        iv_start = root.findViewById(R.id.iv_start);
        tv_start = root.findViewById(R.id.tv_start);

        tv_message = root.findViewById(R.id.tv_message);

        //title
        tv_title_id = root.findViewById(R.id.tv_title_id);
        tv_vnum_id = root.findViewById(R.id.tv_vnum_id);
        //------------------------------

        Glide.with(mActivity).load(bean.getMainpic()).crossFade().placeholder(R.drawable.default_bg).into(img_toppic_id);

        rl_toppic_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (spu.getWifiStatus().equals("wifi")) {
                    VideoPlayerHelper.getInstance().play(rl_toppic_id, bean.getVideourl(), 0);
                } else if (spu.getWifiStatus().equals("mobile")) {
                    if (spu.getWifi().equals("1")) {
                        tv_message.setText("正在使用非wifi网络,播放将产生流量费用");
                        iv_start.setVisibility(View.GONE);
                        tv_start.setVisibility(View.VISIBLE);
                    } else if (spu.getWifi().equals("0")) {
                        if (isneed == true) {
                            rl_toppic_id.setBackgroundColor(mActivity.getResources().getColor(R.color.black));
                            tv_message.setText("正在使用非wifi网络,播放将产生流量费用");
                            iv_start.setVisibility(View.GONE);
                            tv_start.setVisibility(View.VISIBLE);
                        } else if (isneed == false) {
                            tv_start.setVisibility(View.GONE);
                            tv_message.setVisibility(View.GONE);
                            VideoPlayerHelper.getInstance().play(rl_toppic_id, bean.getVideourl(), 0);
                        }
                    }
                }
            }
        });

        tv_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TUtils.toast("当前使用移动网络");
                if (spu.getWifi().equals("1")) {
                    VideoPlayerHelper.getInstance().play(rl_toppic_id, bean.getVideourl(), 0);
                    tv_start.setVisibility(View.GONE);
                    tv_message.setVisibility(View.GONE);
                } else if (spu.getWifi().equals("0")) {
                    VideoPlayerHelper.getInstance().play(rl_toppic_id, bean.getVideourl(), 0);
                    tv_start.setVisibility(View.GONE);
                    tv_message.setVisibility(View.GONE);
                    spu.setIsNeedNotity(false);
                }

            }
        });


        //初始化数据

        tv_title_id.setText(bean.getTitle());
        tv_vnum_id.setText(bean.getVNum() + "次播放");

    }




    //赞
    private void praiseContent() {
        Map<String, String> map = new HashMap<>();
        if (spu.getUser() != null) {
            map.put("uid", spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(mActivity));
        map.put("nid", mbean.getVid());
        map.put("tid", tid);
        map.put("type", "3");
        map.put("siteid", "1");
        Factory.provideHttpService().praiseContent(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {

                        if ("200".equals(baseEntity.code)) {
                            isPraise = true;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }

    public void stopVideo() {
        VideoPlayerHelper.getInstance().stop();
        mActivity.unregisterReceiver(netWorkChangeReceiver);
    }

    @Override
    public void onClick(View view) {

    }
}
